package com.lenovo.newsapp.activity;

import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_AUTHOR;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_ID;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_IMGFRIST;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_IMGSECEND;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_IMGSIZE;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_IMGTHIRD;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_KEYWORD;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_LASTUPDATETIME;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_SUMMARY;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_TABLE;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_THUMBNAIL;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_TITLE;
import static com.lenovo.newsapp.helper.NewsSQLiteOpenHelper.NEWS_TYPE;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import com.adobe.mobile.Config;
import com.lenovo.newsapp.helper.NewsSQLiteOpenHelper;
import com.lenovo.newsapp.model.HomeNews;

public class NewsApplication extends Application {
	private static NewsApplication mInstance;
	private static SQLiteDatabase database;

	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
		NewsSQLiteOpenHelper helper = new NewsSQLiteOpenHelper(this);
		database = helper.getWritableDatabase();
		
		JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
		JPushInterface.init(this);
		// 初始化 JPush
		Set<String> set = new HashSet<String>();
		String s1 = "lenovo_jpush";
		set.add(s1);
		JPushInterface.setAliasAndTags(this,

		s1, null, new TagAliasCallback() {

			@Override
			public void gotResult(int arg0, String arg1,

			Set<String> arg2) {

			}

		});
		
		Config.setContext(this.getApplicationContext());
		Config.setDebugLogging(true);
	}

	public static void setAliasAndTags(Context context, String alias,
			Set<String> tags, TagAliasCallback callback) {

	}
	
	/**
	 * 添加新闻列表private int newsId; private String title; private String summary;
	 * private String newsThumbnail; private String author; private String
	 * keyword; private String lastUpdateTime; private String imgFrist; private
	 * String imgSecend; private String imgThird; private String content;
	 * private String imgSize;
	 */
	public static void addNews(int newsId, String title, String summary,
			String newsThumbnail, String author, String keyword,
			String lastUpdateTime, String imgFrist, String imgSecend,
			String imgThird, String imgSize,int type) {
		
		database.delete(NEWS_TABLE, NEWS_TYPE + "="+type,null);

		ContentValues values = new ContentValues();
		values.put(NEWS_ID, newsId);
		values.put(NEWS_SUMMARY, summary);
		values.put(NEWS_TITLE, title);
		values.put(NEWS_THUMBNAIL, newsThumbnail);
		values.put(NEWS_AUTHOR, author);
		values.put(NEWS_KEYWORD, keyword);
		values.put(NEWS_LASTUPDATETIME, lastUpdateTime);
		values.put(NEWS_IMGFRIST, imgFrist);
		values.put(NEWS_IMGSECEND, imgSecend);
		values.put(NEWS_IMGTHIRD, imgThird);
		values.put(NEWS_IMGSIZE, imgSize);
		values.put(NEWS_TYPE, type);
		database.insert(NEWS_TABLE, null, values);
	}

	/**
	 * 删除数据
	 */
	public static void deleteNews() {
		database.execSQL("delete from news");

	}
	
	public static int test() {
		Cursor messageCursor = database.rawQuery("SELECT * FROM "+NEWS_TABLE,null);
		return messageCursor.getCount();
	}
	/**
	 * 获取数据库的新闻
	 * 
	 * @return
	 */
	public static ArrayList<HomeNews> loadNewsRecords(int type) {
		ArrayList<HomeNews> newsRecords = new ArrayList<HomeNews>();
		Cursor newsCursor = database.query(NEWS_TABLE, new String[] { NEWS_ID,
				NEWS_TITLE, NEWS_SUMMARY, NEWS_THUMBNAIL, NEWS_AUTHOR,
				NEWS_KEYWORD, NEWS_LASTUPDATETIME, NEWS_IMGFRIST,
				NEWS_IMGSECEND, NEWS_IMGTHIRD,NEWS_IMGSIZE },
				NEWS_TYPE + "="+type,null, null, null, String.format("%s desc", NEWS_ID));
		if (newsCursor.getCount() > 0) {
			newsCursor.moveToFirst();
			if (!newsCursor.isAfterLast()) {
				do {

					HomeNews homeNews = new HomeNews();
					homeNews.setNewsId(newsCursor.getInt(0));
					homeNews.setTitle(newsCursor.getString(1));
					homeNews.setSummary(newsCursor.getString(2));
					homeNews.setNewsThumbnail(newsCursor.getString(3));
					homeNews.setAuthor(newsCursor.getString(4));
					homeNews.setKeyword(newsCursor.getString(5));
					homeNews.setLastUpdateTime(newsCursor.getString(6));
					homeNews.setImgFrist(newsCursor.getString(7));
					homeNews.setImgSecend(newsCursor.getString(8));
					homeNews.setImgThird(newsCursor.getString(9));
					homeNews.setImgSize(newsCursor.getString(10));

					newsRecords.add(homeNews);

				} while (newsCursor.moveToNext());
			}
		}
		newsCursor.close();
		return newsRecords;
	}

	@Override
	public void onTerminate() {
		database.close();
		super.onTerminate();
	}

}
